﻿using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 数据管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018.02.03
    /// </summary>
    public class DatabaseBll
    {
        private DatabaseIService service = new DatabaseService();
        private DbLinkBll dbLinkBll = new DbLinkBll();

        /// <summary>
        /// 表列表
        /// </summary>
        /// <param name="linkId">连接Id</param>
        /// <returns></returns>
        public async Task<List<DbTableModel>> GetList(string linkId)
        {
            try
            {
                var link = await dbLinkBll.GetInfo(linkId);
                var tabls = (await service.GetList(link)).FindAll(t => t.Table.ToLower() != "base_tenant" && t.Table.ToLower() != "base_tenantlog");
                return tabls;
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 表字段
        /// </summary>
        /// <param name="linkId">连接Id</param>
        /// <param name="table">表名</param>
        /// <returns></returns>
        public async Task<List<DbTableFieldModel>> GetFieldList(string linkId, string table)
        {
            try
            {
                var link = await dbLinkBll.GetInfo(linkId);
                return await service.GetFieldList(link, table);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 表数据
        /// </summary>
        /// <param name="linkId">连接Id</param>
        /// <param name="table">表名</param>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult> GetData(string linkId, string table, PageInput input)
        {
            try
            {
                var link = await dbLinkBll.GetInfo(linkId);
                return await service.GetData(link, table, input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="linkId">连接Id</param>
        /// <param name="strSql">sql语句</param>
        /// <returns></returns>
        public async Task<int> ExecuteSql(string linkId, string strSql)
        {
            try
            {
                var link = await dbLinkBll.GetInfo(linkId);
                return await service.ExecuteSql(link, strSql);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除表
        /// </summary>
        /// <param name="linkId">连接Id</param>
        /// <param name="table">表名</param>
        public async Task Delete(string linkId, string table)
        {
            try
            {
                var link = await dbLinkBll.GetInfo(linkId);
                await service.Delete(link, table);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建表
        /// </summary>
        /// <param name="linkId">连接Id</param>
        /// <param name="tableModel">表对象</param>
        /// <param name="tableFieldList">字段对象</param>
        public async Task Create(string linkId, DbTableModel tableModel, List<DbTableFieldModel> tableFieldList)
        {
            try
            {
                var link = await dbLinkBll.GetInfo(linkId);
                await service.Create(link, tableModel, tableFieldList);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 修改表
        /// </summary>
        /// <param name="linkId">连接Id</param>
        /// <param name="id">主键值</param>
        /// <param name="tableModel">表对象</param>
        /// <param name="tableFieldList">字段对象</param>
        public async Task Update(string linkId, string id, DbTableModel tableModel, List<DbTableFieldModel> tableFieldList)
        {
            try
            {
                var link = await dbLinkBll.GetInfo(linkId);
                await service.Update(link, id, tableModel, tableFieldList);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}
